﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;

namespace YJ.Data.MSSQL
{
    public class WeiXinMessage : YJ.Data.Interface.IWeiXinMessage
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public WeiXinMessage()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WeiXinMessage实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.WeiXinMessage model)
		{
			string sql = @"INSERT INTO WeiXinMessage
				(ID,ToUserName,FromUserName,CreateTime,CreateTime1,MsgType,MsgId,AgentID,Contents,PicUrl,MediaId,Format,ThumbMediaId,Location_X,Location_Y,Scale,Label,Title,Description,AddTime) 
				VALUES(@ID,@ToUserName,@FromUserName,@CreateTime,@CreateTime1,@MsgType,@MsgId,@AgentID,@Contents,@PicUrl,@MediaId,@Format,@ThumbMediaId,@Location_X,@Location_Y,@Scale,@Label,@Title,@Description,@AddTime)";
			SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID },
				new SqlParameter("@ToUserName", SqlDbType.VarChar, 200){ Value = model.ToUserName },
				new SqlParameter("@FromUserName", SqlDbType.VarChar, 200){ Value = model.FromUserName },
				new SqlParameter("@CreateTime", SqlDbType.Int, -1){ Value = model.CreateTime },
				new SqlParameter("@CreateTime1", SqlDbType.DateTime, 8){ Value = model.CreateTime1 },
				new SqlParameter("@MsgType", SqlDbType.VarChar, 50){ Value = model.MsgType },
				new SqlParameter("@MsgId", SqlDbType.BigInt, -1){ Value = model.MsgId },
				new SqlParameter("@AgentID", SqlDbType.Int, -1){ Value = model.AgentID },
				model.Contents == null ? new SqlParameter("@Contents", SqlDbType.NVarChar, -1) { Value = DBNull.Value } : new SqlParameter("@Contents", SqlDbType.NVarChar, -1) { Value = model.Contents },
				model.PicUrl == null ? new SqlParameter("@PicUrl", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@PicUrl", SqlDbType.VarChar, 500) { Value = model.PicUrl },
				model.MediaId == null ? new SqlParameter("@MediaId", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@MediaId", SqlDbType.VarChar, 500) { Value = model.MediaId },
				model.Format == null ? new SqlParameter("@Format", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Format", SqlDbType.VarChar, 50) { Value = model.Format },
				model.ThumbMediaId == null ? new SqlParameter("@ThumbMediaId", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@ThumbMediaId", SqlDbType.VarChar, 50) { Value = model.ThumbMediaId },
				model.Location_X == null ? new SqlParameter("@Location_X", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Location_X", SqlDbType.VarChar, 50) { Value = model.Location_X },
				model.Location_Y == null ? new SqlParameter("@Location_Y", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Location_Y", SqlDbType.VarChar, 50) { Value = model.Location_Y },
				model.Scale == null ? new SqlParameter("@Scale", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Scale", SqlDbType.VarChar, 50) { Value = model.Scale },
				model.Label == null ? new SqlParameter("@Label", SqlDbType.NVarChar, 1000) { Value = DBNull.Value } : new SqlParameter("@Label", SqlDbType.NVarChar, 1000) { Value = model.Label },
				model.Title == null ? new SqlParameter("@Title", SqlDbType.NVarChar, 1000) { Value = DBNull.Value } : new SqlParameter("@Title", SqlDbType.NVarChar, 1000) { Value = model.Title },
				model.Description == null ? new SqlParameter("@Description", SqlDbType.NVarChar, 2000) { Value = DBNull.Value } : new SqlParameter("@Description", SqlDbType.NVarChar, 2000) { Value = model.Description },
				new SqlParameter("@AddTime", SqlDbType.DateTime, 8){ Value = model.AddTime }
			};
			return dbHelper.Execute(sql, parameters);
		}
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.WeiXinMessage实体类</param>
        public int Update(YJ.Data.Model.WeiXinMessage model)
		{
			string sql = @"UPDATE WeiXinMessage SET 
				ToUserName=@ToUserName,FromUserName=@FromUserName,CreateTime=@CreateTime,CreateTime1=@CreateTime1,MsgType=@MsgType,MsgId=@MsgId,AgentID=@AgentID,Contents=@Contents,PicUrl=@PicUrl,MediaId=@MediaId,Format=@Format,ThumbMediaId=@ThumbMediaId,Location_X=@Location_X,Location_Y=@Location_Y,Scale=@Scale,Label=@Label,Title=@Title,Description=@Description,AddTime=@AddTime
				WHERE ID=@ID";
			SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ToUserName", SqlDbType.VarChar, 200){ Value = model.ToUserName },
				new SqlParameter("@FromUserName", SqlDbType.VarChar, 200){ Value = model.FromUserName },
				new SqlParameter("@CreateTime", SqlDbType.Int, -1){ Value = model.CreateTime },
				new SqlParameter("@CreateTime1", SqlDbType.DateTime, 8){ Value = model.CreateTime1 },
				new SqlParameter("@MsgType", SqlDbType.VarChar, 50){ Value = model.MsgType },
				new SqlParameter("@MsgId", SqlDbType.BigInt, -1){ Value = model.MsgId },
				new SqlParameter("@AgentID", SqlDbType.Int, -1){ Value = model.AgentID },
				model.Contents == null ? new SqlParameter("@Contents", SqlDbType.NVarChar, -1) { Value = DBNull.Value } : new SqlParameter("@Contents", SqlDbType.NVarChar, -1) { Value = model.Contents },
				model.PicUrl == null ? new SqlParameter("@PicUrl", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@PicUrl", SqlDbType.VarChar, 500) { Value = model.PicUrl },
				model.MediaId == null ? new SqlParameter("@MediaId", SqlDbType.VarChar, 500) { Value = DBNull.Value } : new SqlParameter("@MediaId", SqlDbType.VarChar, 500) { Value = model.MediaId },
				model.Format == null ? new SqlParameter("@Format", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Format", SqlDbType.VarChar, 50) { Value = model.Format },
				model.ThumbMediaId == null ? new SqlParameter("@ThumbMediaId", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@ThumbMediaId", SqlDbType.VarChar, 50) { Value = model.ThumbMediaId },
				model.Location_X == null ? new SqlParameter("@Location_X", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Location_X", SqlDbType.VarChar, 50) { Value = model.Location_X },
				model.Location_Y == null ? new SqlParameter("@Location_Y", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Location_Y", SqlDbType.VarChar, 50) { Value = model.Location_Y },
				model.Scale == null ? new SqlParameter("@Scale", SqlDbType.VarChar, 50) { Value = DBNull.Value } : new SqlParameter("@Scale", SqlDbType.VarChar, 50) { Value = model.Scale },
				model.Label == null ? new SqlParameter("@Label", SqlDbType.NVarChar, 1000) { Value = DBNull.Value } : new SqlParameter("@Label", SqlDbType.NVarChar, 1000) { Value = model.Label },
				model.Title == null ? new SqlParameter("@Title", SqlDbType.NVarChar, 1000) { Value = DBNull.Value } : new SqlParameter("@Title", SqlDbType.NVarChar, 1000) { Value = model.Title },
				model.Description == null ? new SqlParameter("@Description", SqlDbType.NVarChar, 2000) { Value = DBNull.Value } : new SqlParameter("@Description", SqlDbType.NVarChar, 2000) { Value = model.Description },
				new SqlParameter("@AddTime", SqlDbType.DateTime, 8){ Value = model.AddTime },
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier, -1){ Value = model.ID }
			};
			return dbHelper.Execute(sql, parameters);
		}
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM WeiXinMessage WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.WeiXinMessage> DataReaderToList(SqlDataReader dataReader)
        {
            List<YJ.Data.Model.WeiXinMessage> List = new List<YJ.Data.Model.WeiXinMessage>();
            YJ.Data.Model.WeiXinMessage model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.WeiXinMessage();
                model.ID = dataReader.GetGuid(0);
                model.ToUserName = dataReader.GetString(1);
                model.CreateTime = dataReader.GetInt32(3);
                model.CreateTime1 = dataReader.GetDateTime(4);
                model.MsgType = dataReader.GetString(5);
                model.MsgId = dataReader.GetInt64(6);
                model.AgentID = dataReader.GetInt32(7);
                if (!dataReader.IsDBNull(8))
                    model.Contents = dataReader.GetString(8);
                if (!dataReader.IsDBNull(9))
                    model.PicUrl = dataReader.GetString(9);
                if (!dataReader.IsDBNull(10))
                    model.MediaId = dataReader.GetString(10);
                if (!dataReader.IsDBNull(11))
                    model.Format = dataReader.GetString(11);
                if (!dataReader.IsDBNull(12))
                    model.ThumbMediaId = dataReader.GetString(12);
                if (!dataReader.IsDBNull(13))
                    model.Location_X = dataReader.GetString(13);
                if (!dataReader.IsDBNull(14))
                    model.Location_Y = dataReader.GetString(14);
                if (!dataReader.IsDBNull(15))
                    model.Scale = dataReader.GetString(15);
                if (!dataReader.IsDBNull(16))
                    model.Label = dataReader.GetString(16);
                if (!dataReader.IsDBNull(17))
                    model.Title = dataReader.GetString(17);
                if (!dataReader.IsDBNull(18))
                    model.Description = dataReader.GetString(18);
                model.AddTime = dataReader.GetDateTime(19);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.WeiXinMessage> GetAll()
        {
            string sql = "SELECT * FROM WeiXinMessage";
            SqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.WeiXinMessage> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM WeiXinMessage";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.WeiXinMessage Get(Guid id)
        {
            string sql = "SELECT * FROM WeiXinMessage WHERE ID=@ID";
            SqlParameter[] parameters = new SqlParameter[]{
				new SqlParameter("@ID", SqlDbType.UniqueIdentifier){ Value = id }
			};
            SqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.WeiXinMessage> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }
    }
}